package com.google.android.mypetrol;

import java.util.Calendar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class PetrolDetail extends LinearLayout implements View.OnFocusChangeListener{

	private Button mOkButton;
	private Button mCancelButton;
	private EditText mTotal;
	private EditText mPrice;
	private EditText mPriceFraction;
	
	private EditText mYear;
	private EditText mMonth;
	private EditText mDate;
	
	private EditText mLiter;
	
	private long mDateMillion;
	
	public PetrolDetail(Context context) {
		super(context);

		LayoutInflater factory = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		factory.inflate(R.layout.new_petrol, this);
		
		mYear = (EditText)findViewById(R.id.year);
		mMonth = (EditText)findViewById(R.id.month);
		mDate = (EditText)findViewById(R.id.date);
	
		mOkButton = (Button)findViewById(R.id.ok);
		mCancelButton = (Button)findViewById(R.id.cancel);
		
		mTotal = (EditText)findViewById(R.id.total);
		mPrice = (EditText)findViewById(R.id.price);
		mPriceFraction = (EditText)findViewById(R.id.price_fraction);
		mLiter = (EditText)findViewById(R.id.liter);
		mTotal.setOnFocusChangeListener(this);
		mPrice.setOnFocusChangeListener(this);
		mPriceFraction.setOnFocusChangeListener(this);
		
		setDate(System.currentTimeMillis());
	}
	
	public void setDate(long date) {
		mDateMillion = date;
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(date);
		mYear.setText("" + c.get(Calendar.YEAR));
		mMonth.setText("" + (c.get(Calendar.MONTH)+ 1));
		mDate.setText("" + c.get(Calendar.DATE));
	}
	
	public void setCost (int total, float price) {
		mTotal.setText("" + total);
		String text = "" + price;
		int index = text.indexOf(".");
		mPrice.setText("" + text.substring(0, index));
		mPriceFraction.setText(text.substring(index+1, text.length()));
		if (total > 0 && price > 0){
			mLiter.setText("" + (total/price));
		}
	}
	
	public void setOnOkClickListener(View.OnClickListener l) {
		mOkButton.setOnClickListener(l);
	}
	
	public void setOnCancelClickListener(View.OnClickListener l) {
		mCancelButton.setOnClickListener(l);
	}
		
	public void onFocusChange(View v, boolean hasFocus) {
		if (!hasFocus) {
			int total = getTotal();
			float price = getPrice();
			
			if (total > 0 && price > 0){
				mLiter.setText("" + (total/price));
			}
		}
	}
	
	public int getTotal() {
		String total = mTotal.getText().toString();
		if (!checkEmpty(total))
			return Integer.parseInt(mTotal.getText().toString());
		else
			return 0;
	}
	
	public float getPrice(){
		String price_fraction = mPriceFraction.getText().toString();
		String price = mPrice.getText().toString();
		if (!checkEmpty(price)) {
			if (!checkEmpty(price_fraction)) {
				price += "." + price_fraction;
			}
		}else {
			return 0;
		}
		return Float.parseFloat(price);
	}
	
	public long getDateMillion() {
		Calendar c = Calendar.getInstance();
		c.set(Integer.parseInt(mYear.getText().toString()),
				Integer.parseInt(mMonth.getText().toString()) - 1, 
				Integer.parseInt(mDate.getText().toString()));
		mDateMillion = c.getTimeInMillis();
		return mDateMillion;
	}
	
	public String getYear() {
		return mYear.getText().toString();
	}
	
	public String getMonth() {
		return mMonth.getText().toString();
	}
	
	public String getDate() {
		return mDate.getText().toString();
	}

	private boolean checkEmpty(String value){
		if (value == null || value.length() == 0)
			return true;
		return false;
	}
}
